//
//  PluPackageTableViewCell.m
//  X-Touch2
//
//  Created by shengchao yang on 12-6-14.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "PluPackageTableViewCell.h"
#import "PluPackageTableViewCellController.h"
#import "Common.h"
#import "Singleton.h"
#import "NSString+Additions.h"
#import "FileManager.h"

@interface PluPackageTableViewCell(Private)
-(void)changeCookNumberNameAndPrice:(NSInteger)type;
-(void)minusNumber;
-(void)addNumber;
@end

@implementation PluPackageTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        PluPackageTableViewCellController *cellController = [[[PluPackageTableViewCellController alloc] initWithNibName:@"PluPackageTableViewCellController" bundle:nil] autorelease];
        [self.contentView addSubview:cellController.view];
        
        btnMinus = (UIButton *)[self viewWithTag:TagOfUIButton];
        btnPkgNumber = (UIButton *)[self viewWithTag:TagOfUIButton + 1];
        btnAdd = (UIButton *)[self viewWithTag:TagOfUIButton + 2];
        btnPkgCook = (UIButton *)[self viewWithTag:TagOfUIButton + 3];//烧法按钮
        [btnPkgCook setTitle:TranslateStr(@"烧法") forState:UIControlStateNormal];
        btnPkgName = (UIButton *)[self viewWithTag:TagOfUIButton + 4];
        [btnPkgName setTitle:TranslateStr(@"菜名") forState:UIControlStateNormal];
        btnPkgPrice = (UIButton *)[self viewWithTag:TagOfUIButton + 5];
        [btnPkgPrice setTitle:TranslateStr(@"单价") forState:UIControlStateNormal];
        btnPkgDel = (UIButton *)[self viewWithTag:TagOfUIButton + 6];
        [btnMinus addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnPkgNumber addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnAdd addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnPkgCook addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnPkgName addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnPkgPrice addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [btnPkgDel addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        
        lblIndex = (UILabel *)[self viewWithTag:TagOfUILabel];
        lblPkgName = (UILabel *)[self viewWithTag:TagOfUILabel + 1];
        lblPkgPrice = (UILabel *)[self viewWithTag:TagOfUILabel + 2];
        lblPkgCook = (UILabel *)[self viewWithTag:TagOfUILabel + 3];
    }
    return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
}
-(void) setData:(id)data index:(NSInteger)index{
    myOrderPluPkg = (MyOrderPluPkgs *)data;
    lblIndex.text = [NSString stringWithFormat:@"%d",index];
    lblPkgName.text = JsonField(myOrderPluPkg.pkgDescripts);
    lblPkgPrice.text = PriceFormatter(myOrderPluPkg.pkgPrice, JsonField(myOrderPluPkg.pkgUnit));
    lblPkgCook.text = myOrderPluPkg.pkgCook;
    [btnPkgNumber setTitle:myOrderPluPkg.pkgNumber forState:UIControlStateNormal];
    
    if ([myOrderPluPkg.pkgTemp isEqualToString:@"1"]) {//如果是临时菜才显示两个按钮
        btnPkgName.hidden = NO;
        btnPkgPrice.hidden = NO;
    }else {//否则就隐藏两个按钮
        btnPkgName.hidden = YES;
        btnPkgPrice.hidden = YES;
    }
}
- (IBAction)buttonClicked:(id)sender{
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
        postN(@"SysMsg_RootView_AlertErrMsg");
        return;
    }
    
    NSInteger bid = [sender tag];
    if (bid == TagOfUIButton) {
        MyNSLog(@"减去1...");        
        NSInteger numBeforeDot = [myOrderPluPkg.pkgNumber intValue];//整数
        if(numBeforeDot == 0){
            alertType = 1;//减1
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                            message:TranslateStr(@"数量已经为0了")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"取消") 
                                                  otherButtonTitles:TranslateStr(@"删除"),nil];
            [alert show];
            [alert release];
        }else {
            [self minusNumber];
            postN(@"SysMsg_PluPackageManagement_refreshPluPkgTable");
        }      
    }else if(bid == TagOfUIButton + 1){
        MyNSLog(@"改数量");
        alertType = 2;//数量
        [self changeCookNumberNameAndPrice:alertType];
    }else if(bid == TagOfUIButton + 2){
        MyNSLog(@"加1...");
        [self addNumber];
        postN(@"SysMsg_PluPackageManagement_refreshPluPkgTable");
    }else if(bid == TagOfUIButton + 3){
        MyNSLog(@"套餐明细的烧法...");
        alertType = 6;//烧法
        [self changeCookNumberNameAndPrice:alertType];
    }else if(bid == TagOfUIButton + 4){
        MyNSLog(@"改菜名");
        alertType = 3;
        [self changeCookNumberNameAndPrice:alertType];
    }else if(bid == TagOfUIButton + 5){
        MyNSLog(@"改单价");
        alertType = 4;
        [self changeCookNumberNameAndPrice:alertType];
    }else if(bid == TagOfUIButton + 6){
        MyNSLog(@"删除...");
        alertType = 5;//删除
        UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                        message:TranslateStr(@"删除确认")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"取消") 
                                              otherButtonTitles:TranslateStr(@"确定"), nil];
        [alert show];
        [alert release];
    }
}
//修改套餐明细的三个属性
-(void) changeCookNumberNameAndPrice:(NSInteger)type{
    UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [myTextField setTag:TagOfUITextField];
    
    NSString * titleName = TranslateStr(@"改数量");
    myTextField.text = myOrderPluPkg.pkgNumber;
    if (type == 3) {
        titleName = TranslateStr(@"改菜名");
        myTextField.text = JsonField(myOrderPluPkg.pkgDescripts);
    }else if(type == 4){
        titleName = TranslateStr(@"改单价");
        myTextField.text = myOrderPluPkg.pkgPrice;
    }else if(type == 6){
        titleName = TranslateStr(@"改烧法");
        myTextField.text = myOrderPluPkg.pkgCook;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleName
                                                    message:@"this gets covered1" 
                                                   delegate:self 
                                          cancelButtonTitle:TranslateStr(@"取消") 
                                          otherButtonTitles:TranslateStr(@"确定"), nil];
    [alert addSubview:myTextField];
    [myTextField release];  
    [alert show];
    [alert release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==alertView.firstOtherButtonIndex){
        if (alertType == 1) {//减1
            [myOrderPluPkg deletePluPkg];
        }else if(alertType == 5) {//删除
            [myOrderPluPkg deletePluPkg];
        }else {
            UITextField *myTextField = (UITextField *)[alertView viewWithTag:TagOfUITextField];            
            if (alertType == 2) {//number
                NSString * newNum = [myTextField.text replaceFrom:@"-" To:@""];//不能点负数            
                [myOrderPluPkg updateNumber:[NSString stringWithFormat:@"%0.2f", [newNum floatValue]]];
            }else if(alertType == 3) {//name
                NSString * oldName = JsonField(myOrderPluPkg.pkgDescripts);
                NSString * newName = Trim(myTextField.text);
                [myOrderPluPkg updateName:[myOrderPluPkg.pkgDescripts replaceFrom:oldName To:newName]];
            }else if(alertType == 4){//price
                [myOrderPluPkg updatePrice:[NSString stringWithFormat:@"%0.2f", [myTextField.text floatValue]]];
            }else if(alertType == 6){
                NSString * newCook = Trim(myTextField.text);
                [myOrderPluPkg updateCook:newCook];
            }
        }
        
        //刷新Table列表
        postN(@"SysMsg_PluPackageManagement_refreshPluPkgTable");
    }
}
-(void)minusNumber{
    NSInteger numBeforeDot = [myOrderPluPkg.pkgNumber intValue];//整数
    NSString * newNumberString = [myOrderPluPkg.pkgNumber replaceFrom:[NSString stringWithFormat:@"%d.",numBeforeDot] To:[NSString stringWithFormat:@"%d.",numBeforeDot-1]];//计算出新的数量
    [myOrderPluPkg updateNumber:newNumberString];
}
-(void)addNumber{
    NSInteger numBeforeDot = [myOrderPluPkg.pkgNumber intValue];//整数
    NSString * newNumberString = [myOrderPluPkg.pkgNumber replaceFrom:[NSString stringWithFormat:@"%d.",numBeforeDot] To:[NSString stringWithFormat:@"%d.",numBeforeDot+1]];//计算出新的数量
    [myOrderPluPkg updateNumber:newNumberString];
}
-(void) dealloc{
    MyNSLog(@"MyOrderTableViewCell dealloc index=%d", self.tag - TagOfUITableViewCell);
    [super dealloc];
}
@end
